<?php


namespace app\api\model;


use think\Model;

class Cart extends Model
{
    /**
     * 关联用户表
     *
     */
    public function user()
    {
        return $this->hasOne('app\api\model\User', 'id', 'user_id')->setEagerlyType(0);
    }

    /**
     * 关联商品表
     *
     */
    public function goods()
    {
        return $this->hasOne('app\api\model\Goods', 'id', 'pro_id')->setEagerlyType(0);
    }

    /**
     * 关联商品规格表
     *
     */
    public function goodsspecs()
    {
        return $this->hasOne('app\api\model\GoodsSpecs', 'id', 'goods_sku_id')->setEagerlyType(0);
    }

    /**
     * 加入购物车
     *
     */
    public function joinCart($user_id, $pro_id, $goods_sku_id, $goods_num)
    {
        $data = [
            'user_id' => $user_id,
            'pro_id' => $pro_id,
            'goods_sku_id' => $goods_sku_id,
            'goods_num' => $goods_num
        ];

        $res = $this->save($data);

        return $res;
    }

    /**
     * 获取购物车内所有商品
     *
     */

    public function getCartAll($user_id)
    {
        $cartall = $this->with('goods_specs')->where('user_id', $user_id)->select();
        return $cartall;
    }
}